﻿using DevComponents.DotNetBar;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.ArcGIS;
using PlanningGIS.Engine.Controls;
using PlanningGIS.Framework;
using PlanningGIS.Util.Log;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.CoordCovert
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmRasterProject : Office2007Form
    {
        /// <summary>
        /// 
        /// </summary>
        public FrmRasterProject()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmRasterProject_Load(object sender, EventArgs e)
        {
            cmbResampling.Items.AddRange(new string[] { "NEAREST", "BILINEAR", "CUBIC", "MAJORITY" });
            cmbResampling.SelectedIndex = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenDataset_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "选择输入栅格数据集文件";
            openFileDialog1.Filter = "IMG文件 (*.img)|*.img|TIF文件 (*.tif)|*.tif|所有文件 (*.*)|*.*";

            if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;
            txtInputDataset.Text = openFileDialog1.FileName;

            ILayer lyr = LayerHelper.GetFileLayer(txtInputDataset.Text);
            if (lyr == null) return;
            IRasterLayer rstlyr = lyr as IRasterLayer;
            ISpatialReference spref = (rstlyr as IGeoDataset).SpatialReference;
            txtInputCoordinate.Text = spref.Name;
            txtInputCoordinate.Tag = spref;
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveDataset_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "选择输出栅格数据集文件";
            saveFileDialog1.Filter = "IMG文件 (*.img)|*.img|TIF文件 (*.tif)|*.tif|所有文件 (*.*)|*.*";
            if (saveFileDialog1.ShowDialog(this) != DialogResult.OK) return;
            txtOutputDataset.Text = saveFileDialog1.FileName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCoordinate_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "选择空间参考文件";
            openFileDialog1.Filter = "坐标文件|*.prj";
            openFileDialog1.Multiselect = false;
            if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;
            txtOutputCoordinate.Text = openFileDialog1.FileName;
            //ISpatialReference spref = PlanningGIS.ArcGIS.GeometryHelper.CreateSpatialReference(openFileDialog1.FileName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                FrameworkHelper.ShowLogPanel();
                if (txtInputDataset.Text.Trim().Length < 2)
                {
                    MsgHelper.ShowInfomationMsgbox("输入栅格数据不能为空！");
                    txtInputDataset.Focus();
                    return;
                }

                if (txtInputCoordinate.Text.Trim().Length < 2)
                {
                    MsgHelper.ShowInfomationMsgbox("输入坐标系统不能为空，否则不能进行投影变换！");
                    txtInputCoordinate.Focus();
                    return;
                }

                if (txtOutputDataset.Text.Trim().Length < 2)
                {
                    MsgHelper.ShowInfomationMsgbox("输出栅格数据不能为空！");
                    txtOutputDataset.Focus();
                    return;
                }

                if (txtOutputCoordinate.Text.Trim().Length < 2)
                {
                    MsgHelper.ShowInfomationMsgbox("输出坐标系统不能为空！");
                    txtOutputCoordinate.Focus();
                    return;
                }
                
                this.Cursor = Cursors.WaitCursor;
                
                LogHelper.WriteLog("正在进行数据投影...");
                ESRI.ArcGIS.DataManagementTools.ProjectRaster gp = new ESRI.ArcGIS.DataManagementTools.ProjectRaster();
                gp.in_raster = this.txtInputDataset.Text;
                gp.out_raster = this.txtOutputDataset.Text;
                gp.out_coor_system = txtOutputCoordinate.Text;
                gp.resampling_type = cmbResampling.SelectedText;
                string msg = "";
                if (!GeoprocessorHelper.Execute(gp, out msg))
                {
                    LogHelper.WriteErrLog("投影失败！" + msg);
                    
                }
                else
                {
                    LogHelper.WriteLog("投影成功！\r\n");
                }                
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLog(ex.Message);
                MsgHelper.ShowWarningMsgbox("投影失败！");
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
